﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using WebResource.Zip;

public partial class Zip_Zip : System.Web.UI.Page
{
    private string ROOTPATH;
    private MyTreeNode ROOT;
    private string OPTION = "-";
    private string OPTIONSTYLE = string.Empty;
    public string treeHTML = string.Empty;
    private string PATH = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        PATH = null == Request["path"] ? "online" : Request["path"].ToString();
        ROOTPATH = Server.MapPath(string.Format("~/{0}/js", PATH));
        ROOT = GetNode(ROOTPATH);
        ROOT.SubNodeList.RemoveAll(delegate(MyTreeNode _node)
        {
            return GetShortName(_node.PathName).StartsWith("min");
        });
        if (OPTION == "+") OPTIONSTYLE = " style=\"display:none;\" ";
        treeHTML = GetTreeHtml(ROOT);
    }

    public string GetShortName(string path)
    {
        string[] _temp = path.Split('\\');
        if (_temp == null || _temp.Length == 0) return string.Empty;
        else return _temp[_temp.Length - 1];
    }

    public string GetTreeHtml(MyTreeNode root)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("<div><h1><a>{1}</a><label><input data-all type=\"checkbox\" />{0}</label></h1><ul{2}>", GetShortName(root.PathName), OPTION, OPTIONSTYLE);
        if (root.SubNodeList != null && root.SubNodeList.Count > 0)
        {
            foreach (MyTreeNode _subNode in root.SubNodeList)
            {
                sb.AppendFormat("<li>{0}</li>", GetTreeHtml(_subNode));
            }
        }
        if (root.FileList != null && root.FileList.Length > 0)
        {
            foreach (string _file in root.FileList)
            {
                sb.AppendFormat("<li><label><input type=\"checkbox\" value=\"{0}\" /><span>{1}</span><label></li>", _file, GetShortName(_file));
            }
        }
        sb.Append("</ul></div>");
        return sb.ToString();
    }



    public MyTreeNode GetNode(string root)
    {
        MyTreeNode thisNode = new MyTreeNode();
        thisNode.PathName = root;
        thisNode.FileList = Directory.GetFiles(root, "*.js", SearchOption.TopDirectoryOnly);
        string[] _sub = Directory.GetDirectories(root);
        if (_sub.Length > 0)
        {
            thisNode.SubNodeList = new List<MyTreeNode>();
            foreach (string _subroot in _sub)
            {
                thisNode.SubNodeList.Add(GetNode(_subroot));
            }
        }
        return thisNode;
    }   
}